package com.rajeshDyadav.classTest;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class DynamicClassLoadingTest02Main {
    DynamicClassLoadingTest02Main() {
	Class myClass;
	Object myObject;

	try {	    
	    myClass = Class.forName("com.rajeshDyadav.classTest.DynamicClassLoadingTest01");
	    myObject = myClass.newInstance();	    

	    System.out.println(myGetMethod(myClass, myObject, "getA"));
	    mySetMethod(myClass, myObject, "setA", 1234);
	    System.out.println(myGetMethod(myClass, myObject, "getA"));
	} catch (Exception e) {
	    e.printStackTrace();
	}
    }

    private Object myGetMethod(Class myClass, Object myObject, String methodName) throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException {
	Method myMethod = myClass.getMethod(methodName, null);
	Object obj = myMethod.invoke(myObject, null);
	return obj;
    }

    private void mySetMethod(Class myClass, Object myObject, String methodName, int parameterValue) throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException {
	Method myMethod = myClass.getMethod("setA", new Class[] { int.class });
	myMethod.invoke(myObject, parameterValue);
    }

    public static void main(String args[]) {
	DynamicClassLoadingTest02Main t = new DynamicClassLoadingTest02Main();
    }
}
